import { Column, Entity, ManyToOne, OneToMany, Index } from 'typeorm';
import { BaseEntity } from '../../../common/entities/base.entity';
import { User } from '../../user/entities/user.entity';
import { Novel } from './novel.entity';

export enum CommentStatus {
  DISABLED = 0,
  ACTIVE = 1,
}

@Entity('comments')
@Index(['novelId'])
@Index(['userId'])
@Index(['parentId'])
@Index(['createdAt'])
export class Comment extends BaseEntity {
  @Column({ type: 'bigint', comment: '用户ID' })
  userId: number;

  @Column({ type: 'bigint', comment: '小说ID' })
  novelId: number;

  @Column({ type: 'bigint', nullable: true, comment: '父评论ID' })
  parentId: number | null;

  @Column({ type: 'text', comment: '评论内容' })
  content: string;

  @Column({ type: 'int', default: 0, comment: '点赞数' })
  likeCount: number;

  @Column({
    type: 'tinyint',
    default: CommentStatus.ACTIVE,
    comment: '状态: 0-禁用 1-正常',
  })
  status: CommentStatus;

  // 关联关系
  @ManyToOne(() => User, (user) => user.comments, { onDelete: 'CASCADE' })
  user: User;

  @ManyToOne(() => Novel, (novel) => novel.comments, { onDelete: 'CASCADE' })
  novel: Novel;

  @ManyToOne(() => Comment, (comment) => comment.replies, {
    onDelete: 'CASCADE',
    nullable: true,
  })
  parent: Comment;

  @OneToMany(() => Comment, (comment) => comment.parent)
  replies: Comment[];
}
